/* app05-window.c
 *
 * Copyright 2024 wan-iot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../app05-config.h"
#include "login-window.h"
#include "../dao/db.h"
#include "../main/main-window.h"
struct _LoginWindow
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
  //GtkLabel            *label;
  GtkEntry *name_input;//账户
  GtkEntry *pwd_input;//密码
  GtkButton *btn_login;//登录按钮
  GtkButton *btn_cancel;//取消按钮
};


G_DEFINE_TYPE (LoginWindow, login_window, GTK_TYPE_APPLICATION_WINDOW)

static void
login_window_class_init (LoginWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/login/login-window.ui");
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, header_bar);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, name_input);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, pwd_input);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, btn_login);
  gtk_widget_class_bind_template_child (widget_class, LoginWindow, btn_cancel);
}
GtkWidget *login_window;
GtkEntry *name_input;
GtkEntry *pwd_input;


gboolean btn_login(){
  //获取账户
  const char* name = (char*)gtk_entry_get_text (name_input);
  // 获取密码
  const char* pwd = (char*)gtk_entry_get_text (pwd_input);
  g_print("name=%s,pwd=%s\n",name,pwd);
  //链接访问数据库
   User *loginUser = malloc(sizeof (User));
 // User* user = &loginUser;
  //memset(user,0,sizeof(loginUser));
  int res = login(name,pwd,loginUser);
  printf("user-->%s\n\n",loginUser->name);
  if(res){
    g_print("登录失败..\n");
  }else{
    g_print("登录成功!\n");
    printf("===>id=%d\n",loginUser->id);
    printf("===>password=%s\n",loginUser->password);
    
    GtkWidget *main_window = (GtkWidget *)g_object_new (MAIN_TYPE_WINDOW, NULL);
    //传值:重要
    g_signal_connect (main_window, "show", G_CALLBACK(emit_signal), loginUser);
    
    gtk_widget_hide (login_window);
    gtk_widget_show_all (main_window);
    //主窗体销毁信号处理
    g_signal_connect (main_window, "destroy", G_CALLBACK(handle_destroy), login_window);
  }
                      
  return TRUE;
}
/*关闭窗体*/
void btn_close(){
  g_print("close..\n");
  gtk_widget_destroy (login_window);
}

static void
login_window_init (LoginWindow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  login_window = (GtkWidget*)self;
  name_input = self->name_input;
  pwd_input = self->pwd_input;
  g_signal_connect (self->btn_login, "clicked", G_CALLBACK(btn_login), NULL);
  g_signal_connect (self->btn_cancel, "clicked", G_CALLBACK(btn_close), NULL);
}
